/**
 * $Id: BaseObject.java 6 2011-03-09 07:12:48Z r.middel@gmail.com $
 *
 * Copyright (C) 2010 mrBussy.eu
 * 
 * WickedHRM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * WickedHRM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.mrbussy.applications.wickedhrm.data;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.UUID;

/**
 * Basic object required for serializing the main classes. Also defining basic attributes
 * like the attribute {@link java.util#Hashtable java.util.Hashtable&lt;K, V&gt;} 
 * @author R. Middel
 * @see {@link java.util#Hashtable java.util.Hashtable&lt;K, V&gt;} 
 *
 */
public class BaseObject {

	protected final DateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd");
	
	private Hashtable<String, String> _attributes;
	protected String getAttribute(String key) { return  _attributes.get(key);}
	protected Calendar getAttributeCalendar(String key) throws ParseException { 
	    Calendar cal= Calendar.getInstance();
		if(this._attributes.get(key)!=null) 
		{
			Date date = dateFormatter.parse(this._attributes.get(key));
		    cal.setTime(date);
		}
		else
			cal=null;
		return cal;
	}
	protected void setAttribute(String key, String value) { _attributes.put(key, value);}
	protected void setAttribute(String key, Calendar value) { 
		Date date = value.getTime();
		this.setAttribute(key, dateFormatter.format(date)); 
	}
	
	private UUID uniqueID;
	protected void setUniqueID(UUID id) { this.uniqueID = id; }
	protected UUID getUniqueID() { return this.uniqueID; }

	public BaseObject() {
		super();
		this._attributes = new Hashtable<String, String>();
		this.uniqueID = UUID.randomUUID();
	}

	public void readObject(ObjectInputStream aInputStream) throws ClassNotFoundException,
			IOException {
			    // always perform the default de-serialization first
			    aInputStream.defaultReadObject();
			    // Custom checks
			} // readObject

	public void writeObject(ObjectOutputStream aOutputStream) throws IOException {
	    //perform the default serialization for all non-transient, non-static fields
	    aOutputStream.defaultWriteObject();
	} // writeObject

}